//
//  WBN_TV_MainViewController.m
//  GoodManNews
//
//  Created by 王博男 on 16/1/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "WBN_TV_MainViewController.h"
#import "WBN_TV_MainModel.h"
#import "WBN_TV_MainTableViewCell.h"
#import "WBN_TV_PlayViewController.h"

#define TVURL @"http://newsvcsp.ifeng.com/vcsp/appData/news/recommend.do?&callBy=news&useType=iPhone&pageSize=20&channelId=000000-0&positionId=0"

@interface WBN_TV_MainViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableV;
@property (nonatomic, retain) NSMutableArray *arr;

@end

@implementation WBN_TV_MainViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.arr = [NSMutableArray array];
    
    self.navigationItem.title = @"TV";
    
    [self createTableV];
    
    [self getData];
}

- (void)createTableV
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    
    
    [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self.view);
        
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    WBN_TV_MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (!cell) {
        cell = [WBN_TV_MainTableViewCell new];
    }
    
    WBN_TV_MainModel *model = [self.arr objectAtIndex:indexPath.row];
    
    [cell.imageV sd_setImageWithURLStr:model.image placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
    cell.titleL.text = model.title;
    cell.contentL.text = model.abstractDesc;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WBN_TV_PlayViewController *playVC = [WBN_TV_PlayViewController new];
    [self.navigationController pushViewController:playVC animated:YES];
}

- (void)getData
{
    [GoodManNews_AFNetTool getNetWithURL:TVURL body:nil headFile:nil responseStyle:WBNJSON success:^(NSURLSessionDataTask *task, id responseObject) {
       
        NSMutableArray *bodyListArr = [responseObject objectForKey:@"bodyList"];
        NSMutableArray *tempArr = [WBN_TV_MainModel baseModelWithArr:bodyListArr];
        for (NSInteger i = 0; i < tempArr.count; i++) {
            
            if (i > 2) {
                [self.arr addObject:tempArr[i]];
            }
            
        }
        
        [self.tableV reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
